<?php

namespace app\common\repository\book;

use think\Db;
use app\common\model\book\Book as BookModel;

class U
{
    public function __construct(
        BookModel $bookModel
    )
    {
        $this->bookModel = $bookModel;
    }

    public function updateBook($entity, $cond)
    {
        $this->bookModel->allowField(true)->save($entity, $cond);
        return $this->bookModel;
    }

    public function updateBookSection($section, $bookId)
    {
        $deleteBookSectionSql = '
            DELETE FROM
                book_section
            WHERE
                book_id = :id
        ';
        Db::execute($deleteBookSectionSql, ['id' => $bookId]);
        BookModel::get($bookId)->section()->saveAll($section);
    }
}
